package com.cbgolf.oa.model;

import android.support.v4.util.ArrayMap;
import com.cbgolf.oa.contract.ICloseParkContract;
import com.cbgolf.oa.entity.Beans;
import com.cbgolf.oa.event.Events;
import com.cbgolf.oa.manager.DataManager;
import com.cbgolf.oa.net.NetCallBack;
import com.cbgolf.oa.net.NetUtils;
import com.cbgolf.oa.net.Web;
import com.cbgolf.oa.net.WebAPI;
import java.util.Map;
import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Author: lq
 * Created on 2018/6/5
 *
 * @author lq
 */
public class CloseParkModelImpl implements ICloseParkContract.Model {


  @Override
  public void addClosePark(Beans beans) {
    if (null == beans) {
      return;
    }
    Map<String, Object> map = new ArrayMap<>();
    JSONObject obj = new JSONObject();
    try {
      obj.put("endTime", beans.endTime);
      obj.put("reason", beans.reason);
      obj.put("remark", beans.remark);
      obj.put("startTime", beans.startTime);
      if (beans.isEdit) {
        obj.put("id", beans.id);
      }
    } catch (JSONException e) {
      e.printStackTrace();
    }
    map.put("closure", obj);
    if (beans.isEdit) {

      editClosePark(map);

    } else {
      createClosePark(map);

    }

  }

  private void createClosePark(Map<String, Object> map) {
    Web.postJsonOk(WebAPI.close_park_create_post, map,
        new NetCallBack(response -> {
          EventBus.getDefault().post(new Events.Builder()
              .msgTarget(DataManager.CLOSE_PARK_CREATE)
              .isSuccess(NetUtils.SUCCESS == response.statusCode)
              .errorCode(0)
              .errorMsg(response.errorMsg)
              .build());
        }));
  }

  private void editClosePark(Map<String, Object> map) {
    Web.putjsonOk(WebAPI.close_park_edit_put, map,
        new NetCallBack(response -> {
          EventBus.getDefault().post(new Events.Builder()
              .msgTarget(DataManager.CLOSE_PARK_EDIT)
              .isSuccess(NetUtils.SUCCESS == response.statusCode)
              .errorCode(0)
              .errorMsg(response.errorMsg)
              .build());
        }));
  }

  @Override
  public void cancelClosePark(String id) {
    Web.putOk(WebAPI.close_park_cancel_put + id, new NetCallBack(response -> {
      EventBus.getDefault().post(new Events.Builder()
          .msgTarget(DataManager.CLOSE_PARK_CANCEL)
          .isSuccess(NetUtils.SUCCESS == response.statusCode)
          .errorCode(0)
          .errorMsg(response.errorMsg)
          .build());
    }));

  }
}